<?php
# 导入数据库接口文件
require_once 'dr-app-config.php';
require_once 'dr-app-functions.php';
require_once 'dr-db-options.php';
require_once 'dr-db-users.php';

# 获得用户的请求
$action = isset($_POST['action']) ? $_POST['action'] : '';
//$action = 'update_user_profile_after_edit';
# 根据用户的请求进行不同的响应
switch ($action) {
	case 'register':
		echo dr_create_new_user();
		break;

	case 'get_user_list':
		echo dr_get_user_list();
		break;

	case 'username_exist':
		echo dr_is_user_exist_with_username();
		break;

	case 'email_exist':
		echo dr_is_user_exist_with_email();
		break;

	case 'change_password':
		echo dr_change_password();
		break;

	case 'set_session':
		echo dr_set_user_session_code();
		break;

	case 'set_login_date':
		echo dr_set_user_login_date();
		break;

	case 'get_userid':
		echo dr_get_user_userid_with_username();
		break;

	case 'get_username':
		echo dr_get_user_username_with_userid();
		break;

	case 'get_passsalt_passcheck':
		echo dr_get_user_passsalt_passcheck();
		break;

	case 'get_session':
		echo dr_get_user_session_code();
		break;

	case 'get_user_profile':
		echo dr_get_user_profile();
		break;

	case 'get_user_profile_for_edit':
		echo dr_get_user_profile_for_edit();
		break;

	case 'update_user_profile_after_edit':
		echo dr_set_user_profile_after_edit();
		break;

	case 'get_user_info':
		echo dr_get_user_info();
		break;

	case 'mark_user':
		echo dr_mark_user();
		break;

	case 'update_reputation':
		echo dr_set_user_reputation();
		break;

	case 'clear_user_record':
		echo dr_clear_user_record();
		break;

	default:
		echo "";
}

/**
 * 创建新的用户
 * @return int register user id
 */
function dr_create_new_user()
{
	global $db;

	# 获得cgi传递的参数
	$username = $_POST['username'];
	$email = $_POST['email'];
	$password = $_POST['password'];

	$salt = dr_random_alphanum(16);

	dr_db_query_sub($db,
	                'INSERT INTO ^users (username,email, passsalt, passcheck,registered_date,last_login_date)' .
	                'VALUES ($,$,$,UNHEX($),date_add(NOW(),interval 8 hour),date_add(NOW(),interval 8 hour))',
	                mysql_real_escape_string($username), mysql_real_escape_string($email), $salt, dr_db_calc_passcheck($password, $salt));

	return '{"userid":' . dr_db_last_insert_id($db) . '}';
}

function dr_get_user_list()
{
	global $db;

	$order = $_POST['order'];
	$user_id = $_POST['user_id'];
	$match_str = $_POST['match_str'];
	$rank = $_POST['rank'];

	$rank_user_num = dr_db_get_option('user_top_num');
	if (empty($rank)) {
		switch ($order) {
			case 'point':
				if (empty($match_str))
					$result = dr_db_query_sub($db, 'select U.user_id,username,registered_date,points,marks_get from ^users U,^record_history R where U.user_id=R.user_id order by points desc,registered_date desc');
				else {
					$match_str .= '%';
					$result = dr_db_query_sub($db, 'select user_id,username,registered_date,points from ^users where username like $ order by points desc,registered_date desc', $match_str);
				}
				break;
			case 'newest':
				if (empty($match_str))
					$result = dr_db_query_sub($db, 'select U.user_id,username,registered_date,points,marks_get from ^users U,^record_history R where U.user_id=R.user_id order by registered_date desc');
				else {
					$match_str .= '%';
					$result = dr_db_query_sub($db, 'select user_id,username,registered_date,points from ^users where username like $ order by registered_date desc', $match_str);
				}
				break;
			case 'oldest':
				if (empty($match_str))
					$result = dr_db_query_sub($db, 'select U.user_id,username,registered_date,points,marks_get from ^users U,^record_history R where U.user_id=R.user_id order by registered_date asc');
				else {
					$match_str .= '%';
					$result = dr_db_query_sub($db, 'select user_id,username,registered_date,points from ^users where username like $ order by registered_date asc', $match_str);
				}
				break;
			case 'name':
				if (empty($match_str))
					$result = dr_db_query_sub($db, 'select U.user_id,username,registered_date,points,marks_get from ^users U,^record_history R where U.user_id=R.user_id order by convert(username using gbk)');
				else {
					$match_str .= '%';
					$result = dr_db_query_sub($db, 'select user_id,username,registered_date,points from ^users where username like $ order by convert(username using gbk)', $match_str);
				}
				break;
			default:
				if (empty($match_str))
					$result = dr_db_query_sub($db, 'select user_id,username,registered_date,points from ^users order by points desc');
				else {
					$match_str .= '%';
					$result = dr_db_query_sub($db, 'select user_id,username,registered_date,points from ^users where username like $ order by points desc', $match_str);
				}
		}
	}
	else
	{
		dr_get_rank_user($result);
	}

	$users = array();
	while ($row = mysql_fetch_assoc($result)) {
		$row['is_marked'] = false;
		if ($user_id) {
			if ($user_id != $row['user_id']) //查看是否已经收听
			{
				$result_mark = dr_db_query_sub($db, 'select * from ^mark_people where user_id=# and marked_user_id=#', $user_id, $row['user_id']);
				if (mysql_num_rows($result_mark) == 0)
					$row['is_marked'] = false;
				else
					$row['is_marked'] = true;
			}
		}
		array_push($users, $row);
	}

	/******************************************************/
	$setting['show_day_top'] = dr_db_get_option('display_day_top');
	$setting['show_week_top'] = dr_db_get_option('display_week_top');
	$setting['show_month_top'] = dr_db_get_option('display_month_top');
	$setting['show_all_top'] = dr_db_get_option('display_all_top');

	$result_array = array();
	$result_array['users'] = $users;
	$result_array['setting'] = $setting;
	$result_array['topNum'] = $rank_user_num;
	return json_encode($result_array);

}

function dr_get_rank_user(&$result)
{
	global $db;
	$rank = $_POST['rank'];
	$interval = 0;
	$start = 0;
	$rank_user_num = dr_db_get_option('user_top_num');
	if (preg_match("/[dwm]5/", $rank)) {
		switch ($rank)
		{
			case 'd5':
				$interval = 3600 * 24 * 1;
				break;
			case 'w5':
				$interval = 3600 * 24 * 7;
				break;
			case 'm5':
				$interval = 3600 * 24 * 30;
				break;
		}
		$result = dr_db_query_sub($db, 'select U.user_id,U.username,U.registered_date,U.points,R.marks_get,count(U.user_id) as add_mark from' .
		                               ' ^users U,^record_history R,^mark_people M where U.user_id=R.user_id and U.user_id=M.marked_user_id and' .
		                               ' unix_timestamp(date_add(now(),interval 8 hour))-unix_timestamp(mark_date)<# group by U.user_id order by add_mark desc limit #,#', $interval, $start, $rank_user_num);

	}
	else if ($rank == 'a5') {
		$result = dr_db_query_sub($db, 'select U.user_id,U.username,U.registered_date,U.points,R.marks_get from' .
		                               ' ^users U,^record_history R where U.user_id=R.user_id order by R.marks_get desc limit #,#', $start, $rank_user_num);
	}
}

function dr_is_user_exist_with_username()
{
	global $db;

	$username = $_POST['username'];

	$result = dr_db_read_one_value(dr_db_query_sub($db, 'select username from ^users where username=$', mysql_real_escape_string($username, $db)));

	if (!isset($result)) {
		return '{"ret":0}';
	}
	return '{"ret":1}';
}

function dr_is_user_exist_with_email()
{
	global $db;

	# 获得cgi传递的参数
	$email = $_POST['email'];

	$result = dr_db_read_one_value(dr_db_query_sub($db, 'select email from ^users where email=$', mysql_real_escape_string($email, $db)));
	if (!isset($result)) {
		return '{"ret":0}';
	}
	return '{"ret":1}';
}

function dr_is_user_exist_with_userid()
{

}

function dr_set_user_session_code()
{
	global $db;

	$userid = $_POST['userid'];
	$session_code = $_POST['session_code'];

	$result = dr_db_query_sub($db,
	                          'UPDATE ^users SET session_code=$ WHERE user_id=#', mysql_real_escape_string($session_code, $db), $userid
	);
	if (!$result) {
		return '{"ret":0}';
	}
	return '{"ret":1}';
}

function dr_get_user_session_code()
{
	global $db;

	$username = $_POST['username'];

	$result = dr_db_read_one_assoc(dr_db_query_sub($db, 'select user_id,session_code from ^users where username=$', mysql_real_escape_string($username, $db)));

	return json_encode($result);
}

function dr_set_user_login_date()
{
	global $db;

	$username = $_POST['username'];

	$result = dr_db_query_sub($db, 'UPDATE ^users SET last_login_date=date_add(NOW(),interval 8 hour) where username=$', mysql_real_escape_string($username, $db));
	if (!$result) {
		return '{"ret":0}';
	}
	return '{"ret":1}';
}

/**
 * 变更用户的密码
 */
function dr_change_password()
{
	global $db;

	$userid = isset($_POST['userid']) ? $_POST['userid'] : 0;
	$username = isset($_POST['username']) ? $_POST['username'] : '';
	$password = isset($_POST['password']) ? $_POST['password'] : '';

	if ($userid) {
		$salt = dr_random_alphanum(16);
		dr_db_query_sub($db,
		                'UPDATE ^users SET passsalt=$, passcheck=UNHEX($) WHERE user_id=#',
		                $salt, dr_db_calc_passcheck($password, $salt), $userid
		);
	} elseif ($username) {
		$salt = dr_random_alphanum(16);
		dr_db_query_sub($db,
		                'UPDATE ^users SET passsalt=$, passcheck=UNHEX($) WHERE username=$',
		                $salt, dr_db_calc_passcheck($password, $salt), mysql_real_escape_string($username)
		);
	} else {
		return '{"ret":0}';
	}
	return '{"ret":1}';
}

function dr_get_user_userid_with_username()
{
	global $db;

	$username = $_POST['username'];

	$result = dr_db_read_one_value(dr_db_query_sub($db, 'select user_id from ^users where username=$', mysql_real_escape_string($username, $db)));
	return '{"userid":' . $result . '}';
}

function dr_get_user_username_with_userid()
{
	global $db;

	$user_id = $_POST['user_id'];

	$result = dr_db_read_one_value(dr_db_query_sub($db, 'select username from ^users where user_id=$', $user_id));

	return '{"username":"' . $result . '"}';
}

function dr_get_user_passsalt_passcheck()
{
	global $db;

	$username = $_POST['username'];

	$result = dr_db_read_one_assoc(dr_db_query_sub($db, 'select passsalt,HEX(passcheck) as passcheck from ^users where username=$', mysql_real_escape_string($username, $db)));

	return json_encode($result);
}

function dr_set_user_reputation()
{
	global $db;

	$user_id = isset($_POST['user_id']) ? intval($_POST['user_id']) : 0;
	$reputation = isset($_POST['reputation']) ? intval($_POST['reputation']) : 0;

	if ($userid === 0 && $reputation === 0) {
		return '{"ret":-1}';
	}

	$result = dr_db_query_sub($db, 'UPDATE ^users SET points=# where user_id=#', $reputation, $user_id);

	if (!$result) {
		return '{"ret":0}';
	}
	return '{"ret":1}';
}

/**
 * 获取用户的个人信息
 */
function dr_get_user_profile()
{
	# 获得传递的数据
	$user_id = $_POST['user_id'];

	# 请求数据库中对应的用户个人信息
	$db_result = dr_db_get_user_profile($user_id);

	#处理获得结果
	$result = array();
	$result['user_id'] = $user_id;
	$result['avatar'] = dr_get_value_with_key($db_result, 'avatar', 'string');
	$result['reputation'] = dr_get_value_with_key($db_result, 'points', 'int');
	$result['role'] = dr_get_role_with_level(dr_get_value_with_key($db_result, 'level', 'int'));
	$result['username'] = dr_get_value_with_key($db_result, 'username', 'string');
	$result['email'] = dr_get_value_with_key($db_result, 'email', 'string');
	$result['displayname'] = dr_get_value_with_key($db_result, 'nickname', 'string');
	$result['memberfor'] = dr_get_value_with_key($db_result, 'registered_date', 'string');
	$result['seen'] = dr_get_value_with_key($db_result, 'last_login_date', 'string');
	$result['website'] = dr_get_value_with_key($db_result, 'website', 'string');
	$result['age'] = dr_get_value_with_key($db_result, 'birthday', 'string');
	$result['gender'] = dr_get_value_with_key($db_result, 'gender', 'string');
	$result['location'] = dr_get_value_with_key($db_result, 'location', 'string');
	$result['aboutme'] = dr_get_value_with_key($db_result, 'aboutme', 'string');
	$result['status'] = dr_get_value_with_key($db_result, 'status', 'int');

	return json_encode($result);
}

function dr_get_user_profile_for_edit()
{
	# 获得传递的数据
	$user_id = $_POST['user_id'];

	# 请求数据库中对应的用户个人信息
	$db_result = dr_db_get_user_profile_for_edit($user_id);

	$result = array();
	$result['user_id'] = $user_id;
	$result['username'] = $db_result['username'];
	$result['nickname'] = $db_result['nickname'];
	$result['email'] = $db_result['email'];
	$result['role'] = dr_get_role_with_level(dr_get_value_with_key($db_result, 'level', 'int'));
	$result['avatar'] = dr_get_value_with_key($db_result, 'avatar', 'string');
	$result['realname'] = dr_get_value_with_key($db_result, 'realname', 'string');
	$result['gender'] = dr_get_value_with_key($db_result, 'gender', 'string');
	$result['birthday'] = dr_get_value_with_key($db_result, 'birthday', 'string');
	$result['website'] = dr_get_value_with_key($db_result, 'website', 'string');
	$result['location'] = dr_get_value_with_key($db_result, 'location', 'string');
	$result['aboutme'] = dr_get_value_with_key($db_result, 'aboutme', 'string');

	return json_encode($result);
}

/**
 * XXX 修改用户的个人信息
 */
function dr_set_user_profile_after_edit()
{
	# 获得传递的数据
	$profile_items = array(
		'username', 'nickname', 'realname', 'email',
		'gender', 'birthday', 'website', 'location', 'aboutme'
	);
	$user_id = $_POST['user_id'];
	$profile = array();
	foreach ($profile_items as $item) {
		$profile[$item] = $_POST[$item];
	}
	$result = dr_db_set_user_profile_after_edit($user_id, $profile);

	if (!$result) {
		return '{"ret":0}';
	}
	return '{"ret":1}';
}

/**
 * 获取用户当前的状态
 */
function dr_get_user_stats()
{

}

/**
 * 获取用户的积分情况
 */
function dr_get_user_reputation()
{

}

/**
 * 获取用户最喜欢的答案
 */
function dr_get_user_favorites()
{

}

/**
 * 获取用户关注的问题
 */
function dr_get_user_prefs()
{

}

/**
 * 获取用户账户管理相关的信息
 */
function dr_get_user_account()
{

}

# ========================================== #
function cmp(&$a, &$b)
{
	$time_a = strtotime($a['action_date']);
	$time_b = strtotime($b['action_date']);
	if ($time_a == $time_b)
		return 0;
	return $time_a < $time_b ? 1 : -1;
}

function dr_get_user_info()
{
	global $db;

	$info = array();

	$user_id = $_POST['user_id'];
	$login_id = $_POST['login_id'];
	$type = $_POST['tab'];
	$msg_id = $_POST['msg_id'];

	if ($msg_id != 0)
		dr_db_query_sub($db, 'delete from ^user_messages where umsg_id=#', $msg_id);

	if ($type == 'message') {
		dr_get_user_message($db, $user_id, $info);
	} elseif ($type == 'activity') {
		dr_get_user_activity($db, $user_id, $info);
	} elseif ($type == 'question') {
		$info['tag'] = array();
		$info['question'] = array();
		$info['mark'] = array();
		dr_get_user_mark_tag($db, $user_id, $login_id, $info['tag']);
		dr_get_user_question($db, $user_id, $login_id, $info['question']);
		dr_get_user_mark($db, $user_id, $login_id, $info['mark']);
	} elseif ($type == 'listen') {
		$info['listening'] = array();
		$info['listened'] = array();
		dr_get_user_listening($db, $user_id, $login_id, $info['listening']);
		dr_get_user_listened($db, $user_id, $login_id, $info['listened']);
	} elseif ($type == 'reply') {
		$info['answer'] = array();
		$info['comment'] = array();
		dr_get_user_answer($db, $user_id, $info['answer']);
		dr_get_user_comment($db, $user_id, $info['comment']);
	} else if ($type == 'vote') {
		$info['question_vote'] = array();
		$info['answer_vote'] = array();
		$info['comment_vote'] = array();
		dr_get_user_question_vote($db, $user_id, $info['question_vote']);
		dr_get_user_answer_vote($db, $user_id, $info['answer_vote']);
		dr_get_user_comment_vote($db, $user_id, $info['comment_vote']);
	} else if ($type == 'statistic') {
		$info['statistic'] = array();
		dr_get_user_statistic($db, $user_id, $info['statistic']);
	}
	echo json_encode($info);
}

function dr_clear_user_record()
{
	global $db;
	$user_id = $_POST['user_id'];
	$clear_type = $_POST['clear_type'];
	if ($clear_type == 'message') {
		dr_db_query_sub($db, 'delete from ^user_messages where user_id=#', $user_id);
	}
	else if ($clear_type == 'action') {
		dr_db_query_sub($db, 'delete from ^action_history where user_id=#', $user_id);
	}
	else
	{
		echo '{"ret":0}';
	}
	echo '{"ret":1}';
}

function cmp_message(&$a, &$b)
{
	$time_a = strtotime($a['created_date']);
	$time_b = strtotime($b['created_date']);
	if ($time_a == $time_b)
		return 0;
	return $time_a < $time_b ? 1 : -1;
}

function dr_get_user_statistic($db, $user_id, &$info)
{
	$result = dr_db_query_sub($db, 'select R.*,U.points from ^record_history R,^users U where R.user_id=U.user_id and R.user_id=#', $user_id);
	while ($row = mysql_fetch_assoc($result))
	{
		array_push($info, $row);
	}

}

function dr_get_user_message($db, $user_id, &$info)
{
	//一般的消息
	$result = dr_db_query_sub($db, 'select M.*, U.username,Q.title as question from ^user_messages M,^users U, ^questions Q'
	                               . ' where M.user_id=# and M.sender_id=U.user_id and M.question_id=Q.question_id order by M.created_date desc', $user_id);
	while ($row = mysql_fetch_assoc($result))
	{
		$row['question'] = dr_cut_string($row['question']);
		array_push($info, $row);
	}

	//关注某个人后，发送给对方的消息
	$result = dr_db_query_sub($db, 'select M.*,U.username from ^user_messages M,^users U where M.user_id=# and' .
	                               ' M.sender_id=U.user_id and type=\'MP\' order by M.created_date desc', $user_id);
	while ($row = mysql_fetch_assoc($result))
	{
		array_push($info, $row);
	}

	usort($info, "cmp_message");

}

function dr_get_user_activity($db, $user_id, &$info)
{
	$day = $_POST['day'];
	$interval = 3600 * 24 * $day;
	//下面的info中只能取到相关问题的title
	$result = dr_db_query_sub($db, 'select act_id,Q.question_id as question_id,action_type,action_date,title as question from ^action_history A,^questions Q' .
	                               ' where A.user_id=# and A.action_type in(\'Q\',\'M\',\'UM\',\'A\',\'VQU\',\'VQD\')and A.object_id=Q.question_id and ' .
	                               'unix_timestamp(date_add(now(),interval 8 hour))-unix_timestamp(A.action_date)<=# order by action_date desc', $user_id, $interval);
	while ($row = mysql_fetch_assoc($result))
	{
		switch ($row['action_type'])
		{
			case 'Q':
				$row['action_type'] = 'question';
				break;
			case 'M':
				$row['action_type'] = 'mark';
				break;
			case 'UM':
				$row['action_type'] = 'cancel mark';
				break;
			case 'A':
				$row['action_type'] = 'answer';
				break;
			case 'VQU':
				$row['action_type'] = 'voteUP';
				break;
			case 'VQD':
				$row['action_type'] = 'voteDown';
				break;
		}
		$row['question'] = dr_cut_string($row['question']);
		array_push($info, $row);
	}

	//下面的info中可以取到问题和相关回答的内容
	$result = dr_db_query_sub($db, 'select act_id,Q.question_id as question_id,action_type,action_date,Q.title as question,R.content as reply ' .
	                               'from ^action_history A,^replies R,^questions Q where A.user_id=# and A.action_type in(\'C\',\'VAU\',\'VAD\',\'S\',\'US\')and' .
	                               ' unix_timestamp(date_add(now(),interval 8 hour))-unix_timestamp(A.action_date)<=# and A.object_id=R.reply_id and' .
	                               ' R.question_id=Q.question_id order by action_date desc', $user_id, $interval);
	while ($row = mysql_fetch_assoc($result))
	{
		switch ($row['action_type'])
		{
			case 'C':
				$row['action_type'] = 'comment';
				break;
			case 'VAU':
				$row['action_type'] = 'voteUP';
				break;
			case 'VAD':
				$row['action_type'] = 'voteDown';
				break;
			case 'S':
				$row['action_type'] = 'Set best';
				break;
			case 'US':
				$row['action_type'] = 'Cancel best';
				break;
		}
		$row['question'] = dr_cut_string($row['question']);
		$row['reply'] = dr_cut_string($row['reply']);
		array_push($info, $row);
	}

	//下面的info中可以取到问题和相关回答和评论的内容
	$result = dr_db_query_sub($db, 'select act_id,Q.question_id as question_id,action_type,action_date,Q.title as question,R.content as comment,parent_id ' .
	                               'from ^action_history A,^replies R,^questions Q where A.user_id=# and A.action_type in(\'VCU\')and A.object_id=R.reply_id and R.question_id=Q.question_id' .
	                               ' and unix_timestamp(date_add(now(),interval 8 hour))-unix_timestamp(A.action_date)<=# order by action_date desc', $user_id, $interval);
	while ($row = mysql_fetch_assoc($result))
	{
		$row['reply'] = dr_db_read_one_value(dr_db_query_sub($db, 'select content from ^replies where reply_id=#', $row['parent_id']));
		switch ($row['action_type'])
		{
			case 'VCU':
				$row['action_type'] = 'voteUP';
				break;
		}
		$row['question'] = dr_cut_string($row['question']);
		$row['reply'] = dr_cut_string($row['reply']);
		$row['comment'] = dr_cut_string($row['comment']);
		array_push($info, $row);
	}

	//下面是info中关注某人的记录
	$result = dr_db_query_sub($db, 'select act_id,action_type,action_date,username,U.user_id from ^action_history A,^users U' .
	                               ' where A.user_id=# and A.action_type in(\'MP\',\'UMP\')and A.object_id=U.user_id ' .
	                               'and unix_timestamp(date_add(now(),interval 8 hour))-unix_timestamp(A.action_date)<=# order by action_date desc', $user_id, $interval);
	while ($row = mysql_fetch_assoc($result))
	{
		switch ($row['action_type'])
		{
			case 'MP':
				$row['action_type'] = 'listen';
				break;
			case 'UMP':
				$row['action_type'] = 'cancel listen';
				break;
		}
		array_push($info, $row);
	}

	//下面是info中关注某tag的记录
	$result = dr_db_query_sub($db, 'select act_id,action_type,action_date,T.title as tag_title,T.tag_id from ^action_history A,^tags T' .
	                               ' where A.user_id=# and A.action_type in(\'MT\',\'UMT\')and A.object_id=T.tag_id' .
	                               ' and unix_timestamp(date_add(now(),interval 8 hour))-unix_timestamp(A.action_date)<=# order by action_date desc', $user_id, $interval);
	while ($row = mysql_fetch_assoc($result))
	{
		switch ($row['action_type'])
		{
			case 'MT':
				$row['action_type'] = 'love';
				break;
			case 'UMT':
				$row['action_type'] = 'cancel love';
				break;
		}
		array_push($info, $row);
	}
	//下面是info中关注某类别的记录

	usort($info, "cmp");
}

function dr_get_user_question($db, $user_id, $login_id, &$info)
{
	$result = dr_db_query_sub($db, 'select Q.*,U.username,U.points from ^questions Q,^users U' .
	                               ' where Q.user_id=# and Q.user_id=U.user_id order by updated_date desc', $user_id);
	$quesion_num = mysql_num_rows($result);

	//读取问题
	while ($row = mysql_fetch_assoc($result))
	{
		$question['id'] = $row['question_id'];
		$question['user_id'] = $row['user_id'];
		$question['title'] = $row['title'];
		$question['user_username'] = $row['username'];
		$question['user_points'] = $row['points'];
		$question['up_votes_num'] = $row['up_votes_num'];
		$question['down_votes_num'] = $row['down_votes_num'];
		$question['answers_num'] = $row['answers_num'];
		$question['marks_num'] = $row['marks_num'];
		$question['created_date'] = $row['created_date'];

		if ($login_id) {
			$result_mark = dr_db_query_sub($db, 'select * from ^mark_question where user_id=# and question_id=#', $login_id, $question['id']);
			if (mysql_num_rows($result_mark) == 0)
				$question['is_mark'] = false;
			else
				$question['is_mark'] = true;
		}
		else
			$question['is_mark'] = false;

		$result_ex = dr_db_query_sub($db, 'select a.tag_id,title from dr_tags a,dr_question_tag b' .
		                                  ' where a.tag_id=b.tag_id and question_id=#', $question['id']);
		$tags = array();
		while ($row_ex = mysql_fetch_assoc($result_ex))
		{
			$tag['id'] = $row_ex['tag_id'];
			$tag['title'] = $row_ex['title'];
			array_push($tags, $tag);
		}
		$question['tags'] = $tags;
		array_push($info, $question);
	}
}

function dr_get_user_mark_tag($db, $user_id, $login_id, &$info)
{

	$result = dr_db_query_sub($db, 'select T.* from ^mark_tag M,^tags T where M.user_id=# and M.marked_tag_id=T.tag_id' .
	                               ' order by M.mark_date desc', $user_id);

	while ($row = mysql_fetch_assoc($result)) {
		$row['is_marked'] = false;
		if ($login_id) {
			$result_mark = dr_db_query_sub($db, 'select * from ^mark_tag where user_id=# and marked_tag_id=#', $login_id, $row['tag_id']);
			if (mysql_num_rows($result_mark) == 0)
				$row['is_marked'] = false;
			else
				$row['is_marked'] = true;
		}
		array_push($info, $row);
	}
}

function dr_get_user_mark($db, $user_id, $login_id, &$info)
{
	$result = dr_db_query_sub($db, 'select Q.*,U.username,U.points from ^questions Q,^mark_question M, ^users U' .
	                               ' where  Q.question_id=M.question_id and M.user_id=# and Q.user_id=U.user_id order by updated_date desc', $user_id);
	$quesion_num = mysql_num_rows($result);

	//读取问题
	while ($row = mysql_fetch_assoc($result))
	{
		$mark['id'] = $row['question_id'];
		$mark['user_id'] = $row['user_id'];
		$mark['title'] = $row['title'];
		$mark['user_username'] = $row['username'];
		$mark['user_points'] = $row['points'];
		$mark['up_votes_num'] = $row['up_votes_num'];
		$mark['down_votes_num'] = $row['down_votes_num'];
		$mark['answers_num'] = $row['answers_num'];
		$mark['marks_num'] = $row['marks_num'];
		$mark['created_date'] = $row['created_date'];

		if ($login_id) {
			$result_mark = dr_db_query_sub($db, 'select * from ^mark_question where user_id=# and question_id=#', $login_id, $mark['id']);
			if (mysql_num_rows($result_mark) == 0)
				$mark['is_mark'] = false;
			else
				$mark['is_mark'] = true;
		}
		else
			$mark['is_mark'] = false;

		$result_ex = dr_db_query_sub($db, 'select a.tag_id,title from dr_tags a,dr_question_tag b' .
		                                  ' where a.tag_id=b.tag_id and question_id=#', $mark['id']);
		$tags = array();
		while ($row_ex = mysql_fetch_assoc($result_ex))
		{
			$tag['id'] = $row_ex['tag_id'];
			$tag['title'] = $row_ex['title'];
			array_push($tags, $tag);
		}
		$mark['tags'] = $tags;
		array_push($info, $mark);
	}
}

function dr_get_user_answer($db, $user_id, &$info)
{

	$result = dr_db_query_sub($db, 'select R.content as reply,Q.title as question,R.updated_date as date,R.question_id as question_id'
	                               . ' from ^replies R,^questions Q where R.user_id=# and R.type=\'A\' and R.question_id=Q.question_id order by R.updated_date desc', $user_id);
	while ($row = mysql_fetch_assoc($result))
	{
		$row['question'] = dr_cut_string($row['question']);
		$row['reply'] = dr_cut_string($row['reply']);
		array_push($info, $row);
	}

}

function dr_get_user_comment($db, $user_id, &$info)
{
	$result = dr_db_query_sub($db, 'select R.content as comment,Q.title as question,parent_id,R.updated_date as date,' .
	                               'R.question_id as question_id from ^replies R,^questions Q where R.user_id=# and R.type=\'C\' and R.question_id=Q.question_id order by R.updated_date desc', $user_id);
	while ($row = mysql_fetch_assoc($result))
	{
		$row['reply'] = dr_db_read_one_value(dr_db_query_sub($db, 'select content from ^replies where reply_id=#', $row['parent_id']));

		$row['question'] = dr_cut_string($row['question']);
		$row['reply'] = dr_cut_string($row['reply']);
		$row['comment'] = dr_cut_string($row['comment']);
		array_push($info, $row);
	}
}

function dr_get_user_question_vote($db, $user_id, &$info)
{
	$result = dr_db_query_sub($db, 'select V.*,Q.title as question,Q.question_id from ^user_votes V,^questions Q '
	                               . 'where vote_type=\'Q\' and V.user_id=# and object_id=Q.question_id order by vote_date desc', $user_id);
	while ($row = mysql_fetch_assoc($result))
	{
		if ($row['updown'] == 1)
			$row['updown'] = 'voteUP';
		else if ($row['updown'] == -1)
			$row['updown'] = 'voteDown';

		$row['question'] = dr_cut_string($row['question']);
		array_push($info, $row);
	}
}

function dr_get_user_answer_vote($db, $user_id, &$info)
{
	$result = dr_db_query_sub($db, 'select V.*,Q.title as question,R.content as reply,Q.question_id from ^user_votes V,^questions Q,^replies R '
	                               . 'where vote_type=\'A\' and V.user_id=# and object_id=R.reply_id and R.question_id=Q.question_id order by vote_date desc', $user_id);
	while ($row = mysql_fetch_assoc($result))
	{
		if ($row['updown'] == 1)
			$row['updown'] = 'voteUP';
		else if ($row['updown'] == -1)
			$row['updown'] = 'voteDown';

		$row['question'] = dr_cut_string($row['question']);
		$row['reply'] = dr_cut_string($row['reply']);
		array_push($info, $row);
	}
}

function dr_get_user_comment_vote($db, $user_id, &$info)
{

	$result = dr_db_query_sub($db, 'select V.*,Q.title as question,R.content as comment,parent_id ,Q.question_id from ^user_votes V,^questions Q,^replies R '
	                               . 'where vote_type=\'C\' and V.user_id=# and object_id=R.reply_id and R.question_id=Q.question_id order by vote_date desc', $user_id);
	while ($row = mysql_fetch_assoc($result))
	{

		if ($row['updown'] == 1)
			$row['updown'] = 'voteUP';
		else if ($row['updown'] == -1)
			$row['updown'] = 'voteDown';

		$row['reply'] = dr_db_read_one_value(dr_db_query_sub($db, 'select content from ^replies where reply_id=#', $row['parent_id']));

		$row['question'] = dr_cut_string($row['question']);
		$row['reply'] = dr_cut_string($row['reply']);
		$row['comment'] = dr_cut_string($row['comment']);
		array_push($info, $row);
	}
}

function dr_get_user_listening($db, $user_id, $login_id, &$info)
{

	$result = dr_db_query_sub($db, 'select U.user_id,U.username from ^mark_people M,^users U where M.user_id=# ' .
	                               'and M.marked_user_id=U.user_id order by M.mark_date desc', $user_id);
	while ($row = mysql_fetch_assoc($result))
	{
		$row['is_marked'] = false;
		if ($login_id && $login_id == $user_id) {
			$row['is_marked'] = true;
		}
		else if ($login_id && $login_id != $user_id) {
			$result_mark = dr_db_query_sub($db, 'select * from ^mark_people where user_id=# and marked_user_id=#', $login_id, $row['user_id']);
			if (mysql_num_rows($result_mark) == 0)
				$row['is_marked'] = false;
			else
				$row['is_marked'] = true;
		}
		array_push($info, $row);
	}

}

function dr_get_user_listened($db, $user_id, $login_id, &$info)
{
	$result = dr_db_query_sub($db, 'select U.user_id,U.username from ^mark_people M,^users U where M.marked_user_id=# ' .
	                               'and M.user_id=U.user_id order by M.mark_date desc', $user_id);
	while ($row = mysql_fetch_assoc($result))
	{
		$row['is_marked'] = false;
		if ($login_id) {
			$result_mark = dr_db_query_sub($db, 'select * from ^mark_people where user_id=# and marked_user_id=#', $login_id, $row['user_id']);
			if (mysql_num_rows($result_mark) == 0)
				$row['is_marked'] = false;
			else
				$row['is_marked'] = true;
		}
		array_push($info, $row);
	}
}

function dr_mark_user()
{
	global $db;

	$user_id = $_POST['user_id'];
	$marked_user_id = $_POST['marked_user_id'];
	$mark_type = $_POST['mark_type'];

	if ($mark_type == 'mark') {
		$result = dr_db_query_sub($db, 'insert into ^mark_people values(#,#,date_add(NOW(),interval 8 hour))', $user_id, $marked_user_id);
		if (!$result) {
			return '{"ret":0}';
		}

		//发送消息给对方，通知已经被关注了
		$result = dr_db_query_sub($db, 'insert into ^user_messages(user_id,sender_id,type,created_date)' .
		                               ' values(#,#,\'MP\',date_add(NOW(),interval 8 hour))', $marked_user_id, $user_id);
		if (!$result) {
			return '{"ret":0}';
		}

		//将此行为记录到用户的历史记录表中
		$result = dr_db_query_sub($db, 'insert into ^action_history(user_id,action_type,action_date,object_id)' .
		                               ' values(#,\'MP\',date_add(NOW(),interval 8 hour),#)', $user_id, $marked_user_id);
		if (!$result) {
			return '{"ret":0}';
		}
	}
	else if ($mark_type == 'cancel_mark') {
		$result = dr_db_query_sub($db, 'delete from ^mark_people where user_id=# and marked_user_id=#', $user_id, $marked_user_id);
		if (!$result) {
			return '{"ret":0}';
		}

		//将此行为记录到用户的历史记录表中
		$result = dr_db_query_sub($db, 'insert into ^action_history(user_id,action_type,action_date,object_id)' .
		                               ' values(#,\'UMP\',date_add(NOW(),interval 8 hour),#)', $user_id, $marked_user_id);
		if (!$result) {
			return '{"ret":0}';
		}
	}
	return '{"ret":1}';
}





# ========================================== #